;(function($){
	var Carousel = function(poster){
		var self=this;
		//保存单个旋转木马对象
		this.poster=poster;
		this.posterItemMain=poster.find("ul.poster-list");
		this.nextBtn=poster.find("div.next-btn");
		this.prevBtn=poster.find("div.prev-btn");
		this.posterItems=poster.find("li.poster-item");
		if(this.posterItems.length%2==0){
			this.posterItemMain.append(this.posterItems.eq(0).clone());
            this.posterItems =this.posterItemMain.children();
		};
		this.posterFirstItem = this.posterItems.first();
		this.posterLastItem =this.posterItems.last();
		this.rotateFlag= true;
		this.direction=0x501;
        
		//默认配置参数
		this.setting = {
			"width":1000,            //幻灯片的宽度
		    "height":270,            //幻灯片的高度
			"posterWidth":640,       //幻灯片第一帧的宽度
			"posterHeight":270,      //幻灯片第一帧的高度
			"verticalAlign":"middle", //记录显示比例关系         
		    "scale":0.9,
		    "speed":500,
		    "autoPlay":false,
		    "delay":2000
		};
		
		$.extend(this.setting,this.getSetting());
		
		//设置参数配置
		this.setSettingValue();
		
		this.setPosterPos();
		
		this.nextBtn.click(function(){
			if(self.rotateFlag){
				self.rotateFlag=false;
				self.direction=0x501;
			    self.carouselRotate("left");
			};
		});
		
		this.prevBtn.click(function(){
			if(self.rotateFlag){
				self.rotateFlag=false;
				self.direction=0x502;
				self.carouselRotate("right");
			};
		});
		
		//是开启否自动播放
		if(this.setting.autoPlay){
			this.autoPlay(self.direction);
			this.poster.hover(function(){
				window.clearInterval(self.timer);
			},function(){
				self.autoPlay(self.direction);
			});
		};
	};
	
	Carousel.prototype={
		//轮播
		autoPlay:function(direction){
			var self=this;
			if(direction===0x501){
			    this.timer=window.setInterval(function(){
				    self.nextBtn.click();
			    },this.setting.delay);
			}else if(direction===0x502){
				this.timer=window.setInterval(function(){
				    self.prevBtn.click();
			    },this.setting.delay);
			};
		},
		
		//旋转
		carouselRotate:function(dir){
			var _this_=this;
			var zIndexArr=[];
			if(dir==="left"){	
				this.posterItems.each(function(){
					
					var self=$(this),
					    prev=self.prev().get(0)?self.prev():_this_.posterLastItem,
					    width = prev.width(),
					    height=prev.height(),
					    zIndex = prev.css("z-index"),
					    opacity=prev.css("opacity"),
					    left=prev.css("left"),
					    top=prev.css("top");
					    zIndexArr.push(zIndex);
					
					self.animate({
						width:width,
						height:height,
						//zIndex:zIndex,
						opacity:opacity,
						left:left,
						top:top
					},_this_.setting.speed,function(){
						_this_.rotateFlag=true;
					});
				});
				
				this.posterItems.each(function(i){
					$(this).css("z-index",zIndexArr[i]);
				});
			}else if(dir==="right"){
				this.posterItems.each(function(){
					
					var self=$(this),
					    next=self.next().get(0)?self.next():_this_.posterFirstItem,
					    width = next.width(),
					    height=next.height(),
					    zIndex = next.css("z-index"),
					    opacity=next.css("opacity"),
					    left=next.css("left"),
					    top=next.css("top");
					    zIndexArr.push(zIndex);
					
					self.animate({
						width:width,
						height:height,
						//zIndex:zIndex,
						opacity:opacity,
						left:left,
						top:top
					},_this_.setting.speed,function(){
						_this_.rotateFlag=true;
				    });
				});
				
				this.posterItems.each(function(i){
					$(this).css("z-index",zIndexArr[i]);
				});
			};
		},
		
		//设置剩余帧的位置关系
		setPosterPos:function(){
			var self=this;
			var sliceItems=this.posterItems.slice(1),
			    sliceSize = sliceItems.length/2,
			    rightSlice=sliceItems.slice(0,sliceSize),
			    level = Math.floor(this.posterItems.length/2),
			    leftSlice = sliceItems.slice(sliceSize);
			    
			var rw=this.setting.posterWidth,
			    rh=this.setting.posterHeight,
			    gap=((this.setting.width-this.setting.posterWidth)/2)/level;
			
			var firstLeft=(this.setting.width-this.setting.posterWidth)/2;
			var fixOffsetLeft=firstLeft+rw;
			
			//设置右边帧的宽度，宽度，top
			rightSlice.each(function(i){
				level--;
			    rw = rw*self.setting.scale;
				rh=rh*self.setting.scale;
				
				var j=i;
				$(this).css({
				    zIndex:level,
				    width:rw,
				    height:rh,
				    left:fixOffsetLeft+(++i)*gap-rw,
				    top:self.setVertucalAlign(rh),
				    opacity:1/(++j)
				});
			});
			
			//设置左边的位置关系
			var lw=rightSlice.last().width(),
			    lh=rightSlice.last().height(),
			    oloop =  Math.floor(this.posterItems.length/2);
			
			leftSlice.each(function(i){
				$(this).css({
				    zIndex:i,
				    width:lw,
				    height:lh,
				    left:i*gap,
				    top:self.setVertucalAlign(lh),
				    opacity:1/oloop
				});	
				lw=lw/self.setting.scale;
				lh=lh/self.setting.scale;
				oloop--;
			});
		},
		
		
		//设置垂直排列对齐
		setVertucalAlign:function(height){
			var verticalType=this.setting.verticalAlign,
			    top=0;
			if(verticalType==="middle"){
				top=(this.setting.height-height)/2;
			}else if(verticalType==="top"){
				top=0;
			}else if(verticalType==="bottom"){
				top=this.setting.height-height;
			}else{
				top=(this.setting.height-height)/2;
			}
			return top;
		},
		
		//设置配置参数值去控制基本的宽度和高度
		setSettingValue:function(){
			this.poster.css({
				width:this.setting.width,
				height:this.setting.height
			});
			
		    this.posterItemMain.css({
		    	width:this.setting.width,
				height:this.setting.height
		    });
		    
		    //计算上下切换按钮的宽度
		    var w=(this.setting.width-this.setting.posterWidth)/2;
		    
		    this.nextBtn.css({
		    	width:w,
		    	height:this.setting.height,
		    	zIndex:Math.ceil(this.posterItems.length/2)
		    });
		    
		    this.prevBtn.css({
		    	width:w,
		    	height:this.setting.height,
		        zIndex:Math.ceil(this.posterItems.length/2)
		    });
		    
		    //设置第一帧的显示位置
		    this.posterFirstItem.css({
		    	width:this.setting.posterWidth,
		    	height:this.setting.oosterHeight,
		    	left:w,
		    	zIndex:Math.floor(this.posterItems.length/2)
		    });
		    
		},
		
		//获取人工配置参数
		getSetting:function(){
			var setting=this.poster.attr("data-setting");
			if(setting&&setting!=""){
				return $.parseJSON(setting);//转换成JSON对象
			}else{
				return {};
			};
		}
	};
	
	Carousel.init=function(posters){
		var _this_ = this;
		posters.each(function(){
			new _this_($(this));
		});
	};
	window["Carousel"] = Carousel;
})($);